package   com.NeuglsWorkStudio.UI.Buttons
{
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**通过图片形成的按钮，要特别注意的是中心点是注册点。
	 * @author Neugls**/
	
	public class TImageButton extends Sprite
	{
		protected const stNormal:int=0;
		protected const stMouseOver:int=1;
		protected const stMouseDown:int=2;
		protected const stDisabled:int=3;
		private var _FTag:Number=-1;
		private var _FImage:DisplayObject;
		private var _FMouseOverImg:DisplayObject=null;
		private var _FMouseDownImg:DisplayObject=null;
		private var _FDisabledImg:DisplayObject=null;
		private var _FImage2:DisplayObject;
		private var _FMouseOverImg2:DisplayObject=null;
		private var _FMouseDownImg2:DisplayObject=null;
		private var _FDisabledImg2:DisplayObject=null;
		private var _FCaption:String;
		private var _Fenabled:Boolean=true;
		private var _FState:int;
		private var _FCaptionField:TextField;
		private var _FFontSize:int;
		private var _FFontBold:Boolean;
		private var _FDisplayCaption:Boolean;
		private var _FDoubleStateButton:Boolean=false;
		private var _FDoubleStateState:Boolean=false;
		protected var _FContainer:Sprite=null;
		private var tmp:DisplayObject=null;
		private var _FToStopPropagation:Boolean=false;

		public function get ToStopPropagation():Boolean
		{
			return _FToStopPropagation;
		}

		public function set ToStopPropagation(value:Boolean):void
		{
			_FToStopPropagation = value;
		}

		public function get DisabledImg2():DisplayObject
		{
			return _FDisabledImg2;
		}

		public function set DisabledImg2(value:DisplayObject):void
		{
			_FDisabledImg2 = value;
		}

		public function get MouseDownImg2():DisplayObject
		{
			return _FMouseDownImg2;
		}

		public function set MouseDownImg2(value:DisplayObject):void
		{
			_FMouseDownImg2 = value;
		}

		public function get MouseOverImg2():DisplayObject
		{
			return _FMouseOverImg2;
		}

		public function set MouseOverImg2(value:DisplayObject):void
		{
			_FMouseOverImg2 = value;
		}

		public function get Image2():DisplayObject
		{
			return _FImage2;
		}

		public function set Image2(value:DisplayObject):void
		{
			_FImage2 = value;
		}

		/**which the button in**/
		public function get DoubleStateState():Boolean
		{
			return _FDoubleStateState;
		}

		/**
		 * @private
		 */
		public function set DoubleStateState(value:Boolean):void
		{
			_FDoubleStateState = value;
			paint();
		}

		/**This function may called at the second state**/
		public function get OnClick2():Function
		{
			return _FOnClick2;
		}

		/**
		 * @private
		 */
		public function set OnClick2(value:Function):void
		{
			_FOnClick2 = value;
		}

		private var _FOnClick2:Function;
		/**Indicate whether this button have two state*/
		public function get DoubleStateButton():Boolean
		{
			return _FDoubleStateButton;
		}

		/**
		 * @private
		 */
		public function set DoubleStateButton(value:Boolean):void
		{
			_FDoubleStateButton = value;
			paint();
		}

		/**
		 * When user click on the button,this function may be called **/
		public function get OnClick():Function
		{
			return _FOnClick;
		}

		/**
		 * @private
		 */
		public function set OnClick(value:Function):void
		{
			_FOnClick = value;
		}
		
		private var _FOnClick:Function=null;
		
		public function TImageButton(Caption:String,DisplayCaption:Boolean=true)
		{
			super();
			_FCaption=Caption;
			_FState=stNormal;
			this.buttonMode=true;
			this.mouseChildren=false;
			_FDisplayCaption=DisplayCaption;
			if(_FDisplayCaption){
				_FCaptionField=new TextField();
				_FCaptionField.autoSize=TextFieldAutoSize.LEFT;
				//_FCaptionField.defaultTextFormat=format;
				_FCaptionField.text=_FCaption;
				addChild(_FCaptionField);
			}
			_FContainer=new Sprite();
			this.addChild(_FContainer);
			
			this.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			this.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
			this.addEventListener(MouseEvent.CLICK,onMouseClick);
		}

		public function get FontBold():Boolean
		{
			return _FFontBold;
		}

		public function set FontBold(value:Boolean):void
		{
			_FFontBold = value;
			if(_FDisplayCaption){
				var format:TextFormat=new TextFormat();
				format.bold=value;
				_FCaptionField.setTextFormat(format);
			}
		}

		public function get FontSize():int
		{
			return _FFontSize;
		}

		public function set FontSize(value:int):void
		{
			_FFontSize = value;
			if(_FDisplayCaption){
				var format:TextFormat=new TextFormat();
				format.size=value;
				_FCaptionField.setTextFormat(format);
			}
		}

		public function get DisabledImg():DisplayObject
		{
			return _FDisabledImg;
		}

		public function set DisabledImg(value:DisplayObject):void
		{
			_FDisabledImg = value;
		}

		public function get MouseDownImg():DisplayObject
		{
			return _FMouseDownImg;
		}

		public function set MouseDownImg(value:DisplayObject):void
		{
			_FMouseDownImg = value;
		}

		public function get MouseOverImg():DisplayObject
		{
			return _FMouseOverImg;
		}

		public function set MouseOverImg(value:DisplayObject):void
		{
			_FMouseOverImg = value;
		}

		public function get enabled():Boolean
		{
			return _Fenabled;
		}

		public function set enabled(value:Boolean):void
		{
			_Fenabled = value;
			_FState=_Fenabled?stNormal:stDisabled;
			paint();
		}

		public function get Caption():String
		{
			return _FCaption;
		}

		public function set Caption(value:String):void
		{
			_FCaption = value;
			if(_FDisplayCaption){
				_FCaptionField.text=value;
				adjustText();
			}
		}

		public function get Tag():Number
		{
			return _FTag;
		}

		public function set Tag(value:Number):void
		{
			_FTag = value;
		}

		public function get Image():DisplayObject
		{
			return _FImage;
		}

		public function set Image(value:DisplayObject):void
		{
			_FImage = value;
			paint();
		}
		
		private function adjustText():void{
			if(!this.contains(_FCaptionField)){
				this.addChildAt(_FCaptionField,this.numChildren);
			}
			_FCaptionField.x=-_FCaptionField.textWidth/2;
			_FCaptionField.y=-_FCaptionField.textHeight/2;
			//_FCaptionField.y=4;
		}
		
		protected function paint():void{
			
			for(var i:int=_FContainer.numChildren-1;i>=0;i--){
				_FContainer.removeChildAt(i);
			}
			/**if(tmp!=null&&_FContainer.contains(tmp))
				_FContainer.removeChild(tmp);**/
			switch(_FState){
				case stNormal:
					tmp=!_FDoubleStateState?_FImage:_FImage2;
					break;
				case stMouseOver:
					tmp=!_FDoubleStateState?_FMouseOverImg:_FMouseOverImg2;
					break;
				case stMouseDown:
					tmp=!_FDoubleStateState?_FMouseDownImg:_FMouseDownImg2;
					break;
				case stDisabled:
					tmp= !_FDoubleStateState?_FDisabledImg:_FDisabledImg2;
					break;
				default:
					tmp=!_FDoubleStateState?_FImage:_FImage2;
			}
			if(tmp!=null){
				with(_FContainer.addChild(tmp)){
					x=-width/2;	
					y=-height/2;	
				}
			}
			if(_FDisplayCaption){
				adjustText();
			}
		}
		
		protected function onMouseOver(event:MouseEvent):void{
			if(_Fenabled){
				_FState=stMouseOver;
				DrawBackground(_FState);
				if(_FMouseOverImg!=null){
					paint();	
				}
			}
		}
		
		protected function onMouseDown(event:MouseEvent):void{
			if(_Fenabled){
				_FState=stMouseDown;
				DrawBackground(_FState);
				if(_FMouseDownImg!=null){
					paint();
				}
			}
		}
		
		protected function onMouseOut(event:MouseEvent):void{
			if(_Fenabled){
				_FState=stNormal;
				DrawBackground(_FState);
				paint();
			}
		}
		
		protected function onMouseUp(event:MouseEvent):void{
			if(_Fenabled){
				_FState=stNormal;
				paint();
			}
		}
		
		protected function onMouseClick(event:MouseEvent):void{
			if(this._FToStopPropagation) event.stopPropagation();
			var f:Function=function(normal:Boolean):void{
				if(normal){
					if(_FOnClick!=null){
						_FOnClick();
					}
				}else{
					if(_FOnClick2!=null){
						_FOnClick2();
					}
				}
			}
			if(this._FDoubleStateButton){
				var d:Boolean=DoubleStateState;
				DoubleStateState=!DoubleStateState;
				d?f(false):f(true);
			}else{
				f(true);
			}
		}
		
		protected function DrawBackground(State:int):void{
			
		}
	}
}